package com.gsy.cs430;


import com.amazonaws.transform.JsonUnmarshallerContextImpl;
import org.ujmp.core.Matrix;

import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Random;

public class HW4 {
    public static void main(String[] args) {
//        getMatrix(0.88d).showGUI();
        showSame();

    }

    public static void showSame(){
        System.out.println("--------------------------------------------------0.85d--------------------------------------------------------");
        for (int i = 0; i < 10; i++) {
            System.out.print(getResult(0.85d,generator(10,1)).toString());
        }
        System.out.println("--------------------------------------------------0.55d--------------------------------------------------------");

        for (int i = 0; i < 10; i++) {
            System.out.print(getResult(0.55d,generator(10,1)).toString());
        }
        System.out.println("--------------------------------------------------1d-----------------------------------------------------------");

        for (int i = 0; i < 10; i++) {
            System.out.print(getResult(1d,generator(10,1)).toString());
        }
    }

    public static double[] generator(int len,double sum){
        Random random = new SecureRandom();
        double re[] = new double[len];
        for (int i = 0; i < len-1; i++) {
            re[i] = random.nextDouble()*sum;
            sum -= re[i];
        }
        re[len-1] = sum;
        return re;
    }

    public static Matrix getMatrix(double dampingNum){
        int []arr = new int[]{13, 16, 17, 19, 23, 25, 27, 29, 31, 32, 37, 41, 43, 47, 49, 53, 59, 61, 64, 67, 71, 73, 79, 81, 83,
                89, 97, 101, 103, 107, 109};
        int vertex[] = new int[10];
        double ma[][] = new double[10][10];
        double dampingParam = dampingNum;
        for (int i : arr) {
            ma[i/10-1][i%10-1] = 1;
        }
        for (double[] doubles : ma) {
            double sum = Arrays.stream(doubles).sum();
            for (int i = 0; i < doubles.length; i++) {
                doubles[i] /= sum;
            }
//            Arrays.stream(doubles).map(operand -> operand/sum).collect(Collectors.toList());
        }
        for (int i = 0; i < vertex.length; i++) {
            vertex[i] = i;
        }
        Matrix matrix = Matrix.Factory.importFromArray(ma);
        Matrix damping = Matrix.Factory.fill(1/10.0,10,10);
        matrix = matrix.times(dampingParam);
        damping = damping.times((1-dampingParam));

        Matrix m = matrix.plus(damping);
//        System.out.println(m.getValueSum());
        Matrix gGraph = Matrix.Factory.copyFromMatrix(m);
        int count = 1;
        while (true){
            count++;
            Matrix temp = Matrix.Factory.copyFromMatrix(gGraph);
            gGraph = gGraph.mtimes(m);
            double aa = Math.abs(temp.minus(gGraph).getAbsoluteValueSum());
//            System.out.println(aa);
            if (aa < 1e-5){
                break;
            }

        }
        System.out.print(count+"         ");
        return gGraph;
    }
    public static Matrix getResult(double dampingNum,double start[]) {
        Matrix v = Matrix.Factory.importFromArray(start);
        Matrix re = v.mtimes(getMatrix(dampingNum));
        return re;
    }
}
